﻿#region License
/* Copyright (C) 2009 Tim Coulter
 * 
 * This file is part of ExtremeML.
 * 
 * ExtremeML is free software: you can redistribute
 * it and/or modify it under the terms of the GNU General Public
 * License, as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 * 
 * This software is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty
 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
 * the GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program. If not, see http://www.gnu.org/licenses/.
*/
#endregion

using System.Collections.Generic;
using DocumentFormat.OpenXml.Packaging;

namespace ExtremeML.Core
{
	/// <summary>
	/// Serves as the base class for classes that maintain collections of <see cref="PartWrapper{TWrapped}"/>.
	/// </summary>
	public abstract class PartWrapperCollection<TMember, TInner> : WrapperCollection<OpenXmlPartContainer, TMember, TInner>
		where TMember : PartWrapper<TInner>
		where TInner : OpenXmlPart
	{
		#region Housekeeping

		/// <summary>
		/// Initializes a new instance of the <see cref="PartWrapperCollection{TMember, TInner}"/> class.
		/// </summary>
		/// <param name="wrapped">An <see cref="OpenXmlPartContainer"/> from which to populate this instance.</param>
		/// <param name="parent">An <see cref="IContentWrapper"/> that represents the parent wrapper instance.</param>
		/// <param name="package">An <see cref="IContentWrapper"/> that represents the package wrapper instance.</param>
		protected PartWrapperCollection(
			OpenXmlPartContainer wrapped,
			IContentWrapper parent,
			IContentWrapper package)
			: base(wrapped, parent, package)
		{
		}

		#endregion
		#region Properties


		#endregion
		#region Overrides


		#endregion
		#region Life-Cycle Methods


		#endregion
		#region Content Management Methods

        /// <summary>
        /// Creates a new wrapper instance, representing a new wrapped content object class and adds it to the collection.
        /// </summary>
        /// <param name="args">A parameter array of <see cref="object"/> containing member initialization arguments.</param>
        /// <returns>A new instance of the member wrapper type.</returns>
        internal TMember Add(
            params object[] args)
		{
			var result = CreateDefaultMemberWrapper(args);
			OnCustomAdding(result);
			Items.Add(result);
			return result;
		}

		/// <summary>
		/// Removes all the members from the collection.
		/// </summary>
		internal override void Clear()
		{
			var items = new List<TMember>(Items);

			foreach (var item in items)
			{
				Remove(item);
			}
		}

		/// <summary>
		/// Removes the passed member from the collection and removes its wrapped part from the package.
		/// </summary>
		/// <param name="member">A <see cref="IContentWrapper"/> to remove.</param>
		internal override void Remove(
			IContentWrapper member)
		{
			if (typeof(TMember).IsAssignableFrom(member.GetType()))
			{
				var target = (TMember)member;

				if (Items.Contains(target) && OnCustomRemoving(target))
				{
					Wrapped.DeletePart(target.Wrapped);
					Items.Remove(target);
					OnCustomRemoved(target);
				}
			}
		}

		#endregion

	    /// <summary>
	    /// When overridden in a derived class, creates a new member wrapper instance populated with default content.
	    /// </summary>
	    /// <param name="args">A parameter array of <see cref="object"/> containing member initialization arguments.</param>
	    /// <returns>A new instance of the member wrapper type.</returns>
	    protected abstract TMember CreateDefaultMemberWrapper(params object[] args);
	}
}
